//
//  DaoHangVC.m
//  DriverClient
//
//  Created by 吕晶 on 2020/8/28.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "DaoHangVC.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import <AMapNaviKit/AMapNaviKit.h>

@interface DaoHangVC ()<CLLocationManagerDelegate,AMapNaviCompositeManagerDelegate>

@property (nonatomic, strong) AMapNaviCompositeManager *compositeManager;

@property (nonatomic, strong) MKMapView *mapView;
@property (nonatomic, strong) UIView *bottomView;
/**
 *  定位管理器
 */
@property (nonatomic,strong) CLLocationManager *locationManager;
/**
 *  地理位置解码编码器
 */
@property (nonatomic,strong) CLGeocoder *geocoder;

@end

@implementation DaoHangVC

- (AMapNaviCompositeManager *)compositeManager {
    if (!_compositeManager) {
        _compositeManager = [[AMapNaviCompositeManager alloc] init];  // 初始化
        _compositeManager.delegate = self;  // 如果需要使用AMapNaviCompositeManagerDelegate的相关回调（如自定义语音、获取实时位置等），需要设置delegate
    }
    return _compositeManager;
}


- (CLLocationManager *)locationManager {
    if (!_locationManager) {
        //创建定位管理器对象，作用是定位当前用户的经度和纬度
        _locationManager = [[CLLocationManager alloc]init];
        _locationManager.delegate = self;
        _locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
        [_locationManager requestAlwaysAuthorization];
        _locationManager.distanceFilter = 10.f;
    }
    return _locationManager;
}

- (CLGeocoder *)geocoder {
    if (!_geocoder) {
        _geocoder = [[CLGeocoder alloc]init];
    }
    return _geocoder;
}

-(UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenH - NaviHeight - 90 - BottomHeight, ScreenW, 90 + BottomHeight)];
        _bottomView.backgroundColor = HexColor(0xffffff);
        UILabel *lab = [UILabel new];
        lab.text = self.address;
        lab.font = [UIFont systemFontOfSize:16];
        lab.textColor = HexColor(0x2B2C44);
        [lab sizeToFit];
        lab.nim_left = 15;
        lab.nim_top = (90 - lab.nim_height) * 0.5;
        lab.nim_width = ScreenW - 30 - 40 - 15;
        [_bottomView addSubview:lab];
        
        UIButton *btn = [UIButton new];
        [btn setImage:[UIImage imageNamed:@"icon_navigation"] forState:0];
        btn.nim_width = 40;
        btn.nim_height = 40;
        btn.nim_left = ScreenW - 15 - 40;
        btn.nim_centerY = lab.nim_centerY;
        [btn addTarget:self action:@selector(daoHangClick) forControlEvents:UIControlEventTouchUpInside];
        [_bottomView addSubview:btn];
    }
    return _bottomView;
}

-(MKMapView *)mapView {
    if (!_mapView) {
        _mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH - NaviHeight)];
        //设置地图的类型
//        _mapView.maskView = MKMapTypeStandard;
        //显示当前用户的位置
        _mapView.showsUserLocation = YES;
    }
    return _mapView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"位置信息";
    [self.view addSubview:self.mapView];
    [self.view addSubview:self.bottomView];
}

- (void)daoHangClick {
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string = self.address;
    [MBProgressHUD leShowSuccessHUDAddedTo:MAIN_WINDOW message:@"地址已复制"];
    [self.compositeManager presentRoutePlanViewControllerWithOptions:nil];  // 通过present的方式显示路线规划页面
//    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
//    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"高德地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        NSString *urlString = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=applicationName&sid=BGVIS1&sname=%@&did=BGVIS2&dname=%@&dev=0&t=0",@"我的位置",self.address] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:nil];
//    }];
//    [alert addAction:action1];
//    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"百度地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=%@,%@&mode=driving",self.lat,self.lng] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//        NSURL *url = [NSURL URLWithString:urlString];
//        [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
//    }];
//    [alert addAction:action2];
//    UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//
//    }];
//    [alert addAction:cancle];
//    [self presentViewController:alert animated:YES completion:nil];
}

@end
